/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.dao.BbsArticleDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class BbsArticleDaoImpl extends BaseDaoImpl implements BbsArticleDao {
	public List getTopList(long columnId, String searchKey, int startIndex,
			int num) {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((a.title like :searchKey) or (a.createrName like :searchKey))";
		}

		String sql = "from com.minierp.model.BbsArticle a where a.parentId=0 and a.columnId="
				+ columnId + str + " order by a.lastTime desc";
		Session sess = null;

		List arg10;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("searchKey", "%" + searchKey + "%");
			}

			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg10 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg10;
	}

	public int getTopListCount(long columnId, String searchKey) {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((a.title like :searchKey) or (a.createrName like :searchKey))";
		}

		String sql = "select count(*) from com.minierp.model.BbsArticle a where a.parentId=0 and a.columnId="
				+ columnId + str;
		Session sess = null;

		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("searchKey", "%" + searchKey + "%");
			}

			List list = q.list();
			if (!list.isEmpty()) {
				int arg9 = Integer.parseInt(list.get(0).toString());
				return arg9;
			}
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return 0;
	}

	public int deleteTree(final long id) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.BbsArticle where id = :id or parentId=:parentId";
						return new Integer(session.createQuery(hql)
								.setLong("id", id).setLong("parentId", id)
								.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public int update(final long id, final long stafferId,
			final String stafferName, final Date createTime) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update com.minierp.model.BbsArticle set replierId=:replierId,replierName=:replierName,lastTime=:lastTime,replyCount=replyCount+1 where id = :id";
						Query q = session.createQuery(hql);
						q.setLong("replierId", stafferId);
						q.setString("replierName", stafferName);
						q.setTimestamp("lastTime", createTime);
						q.setLong("id", id);
						return new Integer(q.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public int update(final long id, final int viewCountInc) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update com.minierp.model.BbsArticle set viewCount=viewCount+"
								+ viewCountInc + " where id = :id";
						Query q = session.createQuery(hql);
						q.setLong("id", id);
						return new Integer(q.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public int update(final long id, final boolean enabled) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update com.minierp.model.BbsArticle set enabled=:enabled where id = :id";
						Query q = session.createQuery(hql);
						if (enabled) {
							q.setString("enabled", "Y");
						} else {
							q.setString("enabled", "N");
						}

						q.setLong("id", id);
						return new Integer(q.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public List<?> getTopList(Date startDate) {
		String sql = "from com.minierp.model.BbsArticle a where a.parentId=0 and a.createTime>:createTime order by id desc";
		Session sess = null;

		List arg5;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			q.setDate("createTime", startDate);
			arg5 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg5;
	}

	public int getCount(long columnId) {
		String sql = "select count(a.id) from com.minierp.model.BbsArticle a where a.columnId="
				+ columnId;
		List list = this.getHibernateTemplate().find(sql);
		return list.size() > 0 && list.get(0) != null ? Integer.parseInt(list
				.get(0).toString()) : 0;
	}
}